package org.example.io.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.example.io.protocol.packet.PacketCodec;

import java.nio.charset.StandardCharsets;

public class ClientMessageHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);

        for (int i = 0; i < 1000; i++) {
            ByteBuf buf = ByteBufAllocator.DEFAULT.ioBuffer();
            byte[] contents = "你好，欢迎关注我的微信公众号，《闪电侠的博 客》!".getBytes(StandardCharsets.UTF_8);
            // 输入魔数
            buf.writeInt(PacketCodec.MAGIC_NUMBER + i);
            buf.writeInt(contents.length);
            buf.writeBytes(contents);
            ctx.writeAndFlush(buf);
        }
    }
}
